package gdr.battle.graphics;


import java.awt.*;
import com.golden.gamedev.object.AnimatedSprite;
import gdr.player.*;

public class GraphicalPlayer {
	protected CoordinateCalculator coordinate;
	
	protected DefaultPlayer player;
	protected AnimatedSprite sprite;
	
	protected HumanGraphicalClient client;
	
	public GraphicalPlayer(){
	}
	
	public void update(long elapsedTime){
		sprite.update(elapsedTime);
		time += elapsedTime;
	}
	
	protected long time = 0;
	double Tx = 600+ Math.random()*400;
	double Ty = 600+ Math.random()*400;
	int diametro = 4;
	
	
	public void render(Graphics2D g){
		render(g, player.getActualHP(), player.getActualMP());
	}
	public void render(Graphics2D g, long printedHP){
		render(g, printedHP, player.getActualMP());
	}
	
	public void render(Graphics2D g, long printedHP, long printedMP){
		//int x = coordinate.getX(player.getPosizione()) - (int)sprite.getWidth()/2;
		//int y = coordinate.getY(player.getPosizione()) - (int)sprite.getHeight() + CoordinateCalculator.UNIT_HEIGHT/2 + 10;
		
		int x = coordinate.getX(player.getPosizione()) + CoordinateCalculator.UNIT_WIDTH/2 - (int)sprite.getWidth()/2;
		int y = coordinate.getY(player.getPosizione()) + CoordinateCalculator.UNIT_HEIGHT/2 - (int)sprite.getHeight()/2;
				
		sprite.setX(x); 
		sprite.setY(y);
		sprite.render(g);
		
		int left_border = 14;
		int right_border = 10;
		int top_border = 12;
		
		// bordo all'interno
		int intBorder = 1;
		//altezza bordo
		int height = 4;
		
		Color colore = null;
		if (client.getClientTeam().equals(player.getTeam())) colore = Color.green;
		else if (client.getClientTeam().getAlly().contains(player.getTeam())) colore = Color.cyan;
		else if (client.getClientTeam().getEnemy().contains(player.getTeam())) colore = Color.magenta;
		else colore = Color.yellow;
		
		//hp
		long hp = printedHP;
		long maxHP = Math.max(getPlayer().getMaxStat(Statistiche.HP), 1);
		drawStat(g, colore, (float)hp/maxHP, left_border, right_border, top_border, intBorder, height);
		//mp
		long mp = printedMP;
		long maxMP = Math.max(getPlayer().getMaxStat(Statistiche.MP),1);
		drawStat(g, colore.darker(), (float)mp/maxMP, left_border, right_border+4, top_border-height, intBorder, height);
		
		g.setColor(Color.black);
		g.fillRect(coordinate.getX(player.getPosizione()) + left_border-2*height-1, 
				   coordinate.getY(player.getPosizione()) + CoordinateCalculator.UNIT_HEIGHT - top_border -height,
				   2*height, 2*height);
		g.setColor(player.getTeam().getColoreTeam());
		g.fillRect(coordinate.getX(player.getPosizione()) + left_border-2*height, 
				   coordinate.getY(player.getPosizione()) + CoordinateCalculator.UNIT_HEIGHT - top_border -height+1,
				   2*height-2, 2*height-2);
		
		/*g.fillRect(coordinate.getX(player.getPosizione()) - CoordinateCalculator.UNIT_WIDTH + left_border-2*height-1, 
				   coordinate.getY(player.getPosizione()) + CoordinateCalculator.UNIT_HEIGHT/2 + top_border,
				   2*height, 2*height);
		g.setColor(player.getTeam().getColoreTeam());
		g.fillRect(coordinate.getX(player.getPosizione()) - CoordinateCalculator.UNIT_WIDTH + left_border-2*height, 
				   coordinate.getY(player.getPosizione()) + CoordinateCalculator.UNIT_HEIGHT/2 + top_border +1,
				   2*height-2, 2*height-2);
		
		/*for (int j = 0; j<2; j++){
			g.setColor(Color.red.brighter().brighter().brighter().brighter().brighter().brighter());
			for (int i=0; i<8; i++){
				int xx = coordinate.resize((int)((CoordinateCalculator.UNIT_WIDTH  - diametro)*(1 + Math.cos(Math.PI*(time)/Tx + 50*i + j*Math.PI))/2 ));
				int yy = coordinate.resize((int)((CoordinateCalculator.UNIT_HEIGHT - diametro)*(1 + Math.sin(Math.PI*(time)/Ty + 50*i + j*Math.PI))/2 ));
				g.fillOval(coordinate.getX(player.getPosizione()) + xx, coordinate.getY(player.getPosizione()) + yy, diametro, diametro);
				g.setColor(g.getColor().darker());
			}
		}*/
	}	

	protected void drawStat(Graphics2D g, Color color, float value, int left_border, int right_border, int top_border, int intBorder, int height){
		//int x = coordinate.getX(player.getPosizione()) - CoordinateCalculator.UNIT_WIDTH + left_border;
		//int y = coordinate.getY(player.getPosizione()) + CoordinateCalculator.UNIT_HEIGHT/2 + top_border;
		
		int x = coordinate.getX(player.getPosizione()) + left_border;
		int y = coordinate.getY(player.getPosizione()) + CoordinateCalculator.UNIT_HEIGHT - top_border - height;		
		int w = CoordinateCalculator.UNIT_WIDTH - left_border - right_border;
		int h = height;
		g.setColor(Color.black);
		g.fillRect(x,y, w,h);
		x += intBorder;
		y += intBorder;
		w -= 2*intBorder;
		w = Math.round(value*w);
		h = height - 2*intBorder;
		g.setColor(color);
		g.fillRect(x,y, w,h);
	}


	public DefaultPlayer getPlayer() {
		return player;
	}

	public void setPlayer(DefaultPlayer player) {
		this.player = player;
	}

	public AnimatedSprite getSprite() {
		return sprite;
	}

	public void setSprite(AnimatedSprite sprite) {
		this.sprite = sprite;
	}

	public HumanGraphicalClient getClient() {
		return client;
	}

	public void setClient(HumanGraphicalClient client) {
		this.client = client;
	}

	public CoordinateCalculator getCoordinate() {
		return coordinate;
	}

	public void setCoordinate(CoordinateCalculator coordinate) {
		this.coordinate = coordinate;
	}
}
